package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.Vo.GoodsDetailVo;
import cn.leyaber.hrm.clients.UserFeignClient;
import cn.leyaber.hrm.domain.GoodsDetail;
import cn.leyaber.hrm.domain.Wholename;
import cn.leyaber.hrm.dto.entity.SfGoodsDetails;
import cn.leyaber.hrm.fegin.GoodsBasicService;
import cn.leyaber.hrm.query.GoodsDetailQuery;
import cn.leyaber.hrm.service.IGoodsDetailService;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/goodsDetail")
public class GoodsDetailController {
    @Autowired
    public IGoodsDetailService goodsDetailService;
    @Autowired
    private GoodsBasicService goodsBasicService;
    @Autowired
    private UserFeignClient userFeignClient;
   @PatchMapping(value=" /batchDelete")
   /**
    * 批量删除
    */
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            goodsDetailService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 修改状态
     * @param goodsDetail
     * @return
     */
    @PostMapping(value="/Modify_status")
    public AjaxResult Modify_status(@RequestBody GoodsDetail goodsDetail){
        try {
            goodsDetailService.Modify_status( goodsDetail);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("修改对象失败！"+e.getMessage());
        }
    }

    /**
    * 保存和修改公用的
    * @param goodsDetail  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody GoodsDetail goodsDetail){
        try {
            if(goodsDetail.getId()!=null){
                goodsDetailService.updateById(goodsDetail);
            }else{
                goodsDetailService.insert(goodsDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            goodsDetailService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public GoodsDetail get(@PathVariable("id")Long id)
    {
        return goodsDetailService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<GoodsDetail> list(){

        return goodsDetailService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<GoodsDetailVo> json(@RequestBody GoodsDetailQuery query)
    {
        Page<GoodsDetail> page = new Page<GoodsDetail>(query.getPage(),query.getRows());
        page = goodsDetailService.selectPage(page);
        Page<GoodsDetailVo> page1 = JSON.parseObject(JSON.toJSONString(page), Page.class);
        ArrayList<GoodsDetailVo> objects = new ArrayList<>();
        for (GoodsDetail record : page.getRecords()) {
            GoodsDetailVo goodsDetailVo = JSON.parseObject(JSON.toJSONString(record), GoodsDetailVo.class);
            if (StringUtils.isEmpty(record.getUserWholenameId())&&StringUtils.isEmpty(record.getSfGoodsDetailsId())){
                objects.add(goodsDetailVo);
                continue;
            }
            Wholename wholename = userFeignClient.selectById(record.getUserWholenameId());
            SfGoodsDetails byId = goodsBasicService.getById(record.getSfGoodsDetailsId());
            goodsDetailVo.setGoodsName(byId.getGoodsName());
            goodsDetailVo.setUser(wholename.getUser());
            objects.add(goodsDetailVo);
        }
        page1.setRecords(objects);
        return new PageList<GoodsDetailVo>(page.getTotal(),page1.getRecords());
    }
}
